﻿//Table:S_RiZhiXX

#region Entity

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Entity;
using Platform.Configuration;
using Platform.Data;

namespace Entity
{
    /// <summary>
    ///     日志信息
    /// </summary>
    [Serializable]
    public class S_RiZhiXX
    {
        private DateTime? m_d_caozuorq;

        private int? m_i_sid;
        private int m_id;

        private string m_s_biaoming;

        private string m_s_buzhoubh;

        private string m_s_caozuolx;

        private string m_s_caozuor;

        private string m_s_ipdizhi;

        private string m_s_rizhinr;

        private string m_s_suoshumk;

        private string m_s_xiangmubh;

        private string m_s_zhujian;

        /// <summary>
        ///     自增量
        /// </summary>
        public int ID
        {
            get { return m_id; }
            set { m_id = value; }
        }

        /// <summary>
        ///     项目ID
        /// </summary>
        public int? I_SID
        {
            get { return m_i_sid; }
            set { m_i_sid = value; }
        }

        /// <summary>
        ///     步骤编号
        /// </summary>
        public string S_BuZhouBH
        {
            get { return m_s_buzhoubh; }
            set { m_s_buzhoubh = value; }
        }

        /// <summary>
        ///     项目编号
        /// </summary>
        public string S_XiangMuBH
        {
            get { return m_s_xiangmubh; }
            set { m_s_xiangmubh = value; }
        }

        /// <summary>
        ///     所属模块
        /// </summary>
        public string S_SuoShuMK
        {
            get { return m_s_suoshumk; }
            set { m_s_suoshumk = value; }
        }

        /// <summary>
        ///     表名
        /// </summary>
        public string S_BiaoMing
        {
            get { return m_s_biaoming; }
            set { m_s_biaoming = value; }
        }

        /// <summary>
        ///     主键
        /// </summary>
        public string S_ZhuJian
        {
            get { return m_s_zhujian; }
            set { m_s_zhujian = value; }
        }

        /// <summary>
        ///     IP地址
        /// </summary>
        public string S_IPDiZhi
        {
            get { return m_s_ipdizhi; }
            set { m_s_ipdizhi = value; }
        }

        /// <summary>
        ///     日志内容
        /// </summary>
        public string S_RiZhiNR
        {
            get { return m_s_rizhinr; }
            set { m_s_rizhinr = value; }
        }

        /// <summary>
        ///     操作人
        /// </summary>
        public string S_CaoZuoR
        {
            get { return m_s_caozuor; }
            set { m_s_caozuor = value; }
        }

        /// <summary>
        ///     操作日期
        /// </summary>
        public DateTime? D_CaoZuoRQ
        {
            get { return m_d_caozuorq; }
            set { m_d_caozuorq = value; }
        }

        /// <summary>
        ///     操作类型
        /// </summary>
        public string S_CaoZuoLX
        {
            get { return m_s_caozuolx; }
            set { m_s_caozuolx = value; }
        }
    }
}

#endregion

#region EntityProcess

namespace BLL
{
    public abstract class S_RiZhiXXProcess
    {
        #region Field

        protected string conn;

        #endregion

        #region Construction

        public S_RiZhiXXProcess()
        {
            conn = ConfigHelper.PlatformConnString;
        }

        #endregion

        #region public

        public bool InsertOrUpdate(S_RiZhiXX S_RiZhiXX, DbTransaction Transaction)
        {
            if (S_RiZhiXX.ID == 0)
                InsertS_RiZhiXX(S_RiZhiXX, Transaction);
            else
                UpdateS_RiZhiXX(S_RiZhiXX, Transaction);
            return S_RiZhiXX.ID == 0 ? false : true;
        }

        public bool DeleteS_RiZhiXX(int ID, DbTransaction Transaction)
        {
            var spName = "_DeleteS_RiZhiXX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = ID;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            return result == 0 ? false : true;
        }

        public bool DeleteS_RiZhiXXesDynamic(string WhereClause, DbTransaction Transaction)
        {
            var spName = "_DeleteS_RiZhiXXesDynamic";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = WhereClause;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            return result == 0 ? false : true;
        }

        public DataTable LeftJoin<EntityName>(string On, string WhereClause, string OrderByExpression,
            DbTransaction Transaction)
        {
            DataSet ds = null;
            if (Transaction == null)
                ds = SqlHelper.ExecuteDataset(conn, CommandType.Text,
                    "select * from S_RiZhiXX left join " + typeof(EntityName).Name + " on " + On +
                    (string.IsNullOrEmpty(WhereClause.Trim()) ? "" : " where " + WhereClause) + " " +
                    (string.IsNullOrEmpty(OrderByExpression.Trim()) ? "" : " order by " + OrderByExpression));
            else
                ds = SqlHelper.ExecuteDataset((SqlTransaction) Transaction, CommandType.Text,
                    "select * from S_RiZhiXX left join " + typeof(EntityName).Name + " on " + On +
                    (string.IsNullOrEmpty(WhereClause.Trim()) ? "" : " where " + WhereClause) + " " +
                    (string.IsNullOrEmpty(OrderByExpression.Trim()) ? "" : " order by " + OrderByExpression));
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        public S_RiZhiXX SelectS_RiZhiXX(int ID, DbTransaction Transaction)
        {
            var spName = "_SelectS_RiZhiXX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = ID;
            SqlDataReader rd = null;
            S_RiZhiXX _S_RiZhiXX = null;
            if (Transaction == null)
                rd = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                rd = SqlHelper.ExecuteReader((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            if (rd.Read())
                _S_RiZhiXX = PopulateS_RiZhiXX(rd);
            if (rd != null)
                rd.Close();
            return _S_RiZhiXX;
        }

        public DataTable SelectS_RiZhiXXesAll(DbTransaction Transaction)
        {
            var spName = "_SelectS_RiZhiXXesAll";
            DataSet ds = null;
            if (Transaction == null)
                ds = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, spName);
            else
                ds = SqlHelper.ExecuteDataset((SqlTransaction) Transaction, CommandType.StoredProcedure, spName);
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        public DataTable SelectS_RiZhiXXesDynamic(string WhereClause, string OrderByExpression,
            DbTransaction Transaction)
        {
            var spName = "_SelectS_RiZhiXXesDynamic";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = string.IsNullOrEmpty(WhereClause.Trim()) ? "1=1" : WhereClause;
            storedParams[1].Value = OrderByExpression;
            DataSet ds = null;
            if (Transaction == null)
                ds = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                ds = SqlHelper.ExecuteDataset((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        public DataTable SelectS_RiZhiXXesBySerialNumberID(int SerialNumberID, string OrderByExpression,
            DbTransaction Transaction)
        {
            return SelectS_RiZhiXXesDynamic("SerialnumberID=" + SerialNumberID, OrderByExpression, Transaction);
        }

        #endregion

        #region Private

        private int InsertS_RiZhiXX(S_RiZhiXX S_RiZhiXX, DbTransaction Transaction)
        {
            var spName = "_InsertS_RiZhiXX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = S_RiZhiXX.I_SID;
            storedParams[1].Value = S_RiZhiXX.S_BuZhouBH;
            storedParams[2].Value = S_RiZhiXX.S_XiangMuBH;
            storedParams[3].Value = S_RiZhiXX.S_SuoShuMK;
            storedParams[4].Value = S_RiZhiXX.S_BiaoMing;
            storedParams[5].Value = S_RiZhiXX.S_ZhuJian;
            storedParams[6].Value = S_RiZhiXX.S_IPDiZhi;
            storedParams[7].Value = S_RiZhiXX.S_RiZhiNR;
            storedParams[8].Value = S_RiZhiXX.S_CaoZuoR;
            storedParams[9].Value = S_RiZhiXX.D_CaoZuoRQ;
            storedParams[10].Value = S_RiZhiXX.S_CaoZuoLX;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            S_RiZhiXX.ID = (int) storedParams[11].Value;
            return result;
        }

        private bool UpdateS_RiZhiXX(S_RiZhiXX S_RiZhiXX, DbTransaction Transaction)
        {
            var spName = "_UpdateS_RiZhiXX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = S_RiZhiXX.ID;
            storedParams[1].Value = S_RiZhiXX.I_SID;
            storedParams[2].Value = S_RiZhiXX.S_BuZhouBH;
            storedParams[3].Value = S_RiZhiXX.S_XiangMuBH;
            storedParams[4].Value = S_RiZhiXX.S_SuoShuMK;
            storedParams[5].Value = S_RiZhiXX.S_BiaoMing;
            storedParams[6].Value = S_RiZhiXX.S_ZhuJian;
            storedParams[7].Value = S_RiZhiXX.S_IPDiZhi;
            storedParams[8].Value = S_RiZhiXX.S_RiZhiNR;
            storedParams[9].Value = S_RiZhiXX.S_CaoZuoR;
            storedParams[10].Value = S_RiZhiXX.D_CaoZuoRQ;
            storedParams[11].Value = S_RiZhiXX.S_CaoZuoLX;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            return result == 0 ? false : true;
        }

        private S_RiZhiXX PopulateS_RiZhiXX(IDataReader dr)
        {
            var S_RiZhiXX = new S_RiZhiXX();
            S_RiZhiXX.ID = dr["ID"] == DBNull.Value ? 0 : (int) dr["ID"];
            S_RiZhiXX.I_SID = dr["I_SID"] == DBNull.Value ? null : (int?) dr["I_SID"];
            S_RiZhiXX.S_BuZhouBH = dr["S_BuZhouBH"] == DBNull.Value ? null : (string) dr["S_BuZhouBH"];
            S_RiZhiXX.S_XiangMuBH = dr["S_XiangMuBH"] == DBNull.Value ? null : (string) dr["S_XiangMuBH"];
            S_RiZhiXX.S_SuoShuMK = dr["S_SuoShuMK"] == DBNull.Value ? null : (string) dr["S_SuoShuMK"];
            S_RiZhiXX.S_BiaoMing = dr["S_BiaoMing"] == DBNull.Value ? null : (string) dr["S_BiaoMing"];
            S_RiZhiXX.S_ZhuJian = dr["S_ZhuJian"] == DBNull.Value ? null : (string) dr["S_ZhuJian"];
            S_RiZhiXX.S_IPDiZhi = dr["S_IPDiZhi"] == DBNull.Value ? null : (string) dr["S_IPDiZhi"];
            S_RiZhiXX.S_RiZhiNR = dr["S_RiZhiNR"] == DBNull.Value ? null : (string) dr["S_RiZhiNR"];
            S_RiZhiXX.S_CaoZuoR = dr["S_CaoZuoR"] == DBNull.Value ? null : (string) dr["S_CaoZuoR"];
            S_RiZhiXX.D_CaoZuoRQ = dr["D_CaoZuoRQ"] == DBNull.Value ? null : (DateTime?) dr["D_CaoZuoRQ"];
            S_RiZhiXX.S_CaoZuoLX = dr["S_CaoZuoLX"] == DBNull.Value ? null : (string) dr["S_CaoZuoLX"];
            return S_RiZhiXX;
        }

        private void AddRowNumber(DataTable dt)
        {
            dt.Columns.Add(new DataColumn("RowNumber", typeof(int)));
            for (var i = 0; i < dt.Rows.Count; i++)
                dt.Rows[i][dt.Columns.Count - 1] = i + 1;
        }

        #endregion
    }
}

#endregion